package com.ikun.common.easyexcel.converters;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.util.Objects;

/**
 * @ClassName CommonStatusConverter
 * @Description:
 * @CreateDate: 2024/8/6 9:57
 */
public class CommonStatusConverter implements Converter<Integer> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
      String cellStr = cellData.getStringValue();
      if(Objects.equals(cellStr,"正常")){
          return 0;
      }else if(Objects.equals(cellStr,"停用")){
          return 1;
      }else{
          return 2;
      }
    }

    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (value == 0) {
            return new WriteCellData<>("正常");
        } else if (value == 1) {
            return new WriteCellData<>("停用");
        } else {
            return new WriteCellData<>("未知");
        }
    }
}
